package com.huan.filter;

import com.huan.pojo.User;
import com.huan.util.Constants;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
 * 作者： 王鹏欢
 * @Date 2021-07-20 0:36
 * @Description 过滤器
 */
public class SysFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {
        //ServletRequest中没有Session所以要转为HttpServletRequest
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        //过滤器从Session中获取对象
        User user = (User) request.getSession().getAttribute(Constants.USER_SESSION);
        if (user==null){
            //已经移除或注销，跳转到error页面
            response.sendRedirect("/smbms/error.jsp");
        }else {
            //让我们的请求继续走，如果不写，程序到这里就被拦截停止！
            filterChain.doFilter(req,resp);
        }
    }

    @Override
    public void destroy() {

    }
}
